package cn.soft863.servlet.head;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.soft863.bike.pojo.ABike;
import cn.soft863.bike.service.IBikeService;
import cn.soft863.bike.service.impl.BikeServiceImpl;
import cn.soft863.common.servlet.BaseServlet;
import cn.soft863.orders.pojo.AOrders;
import cn.soft863.orders.service.IOrdersService;
import cn.soft863.orders.service.impl.OrdersServiceImpl;
import cn.soft863.users.pojo.AUsers;

/**
 * 车辆Servlet,前台
 * @author 902-B-Book
 *
 */
public class OrdersHeadServlet extends BaseServlet
{
	private IBikeService bikeService = new BikeServiceImpl();
	private IOrdersService ordersService = new OrdersServiceImpl();
	/**
	 * Service方法
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 车辆列表 */
		String method = request.getParameter("method");
		if("ordersInsert".equalsIgnoreCase(method))
		{
			ordersInsert(request,response);
		}else if("ordersInsertSubmit".equalsIgnoreCase(method))
		{
			ordersInsertSubmit(request,response);
		}else if("OrdersEnd".equalsIgnoreCase(method))
		{
			OrdersEnd(request,response);
		}
		
		
	}

	private void OrdersEnd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======ordersEnd============");
		String orderid = request.getParameter("orderid");
		String orderHisid = request.getParameter("orderHisid");
		HttpSession session = request.getSession() ; 
		AUsers users = (AUsers) session.getAttribute("users");
		AOrders orders = new AOrders() ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("orderid", orderid);
		orders = this.ordersService.findOneorderService(condMap);
		orders.setEndTime(new Date());
	    int x = this.ordersService.updateOneorderService(orders);
		if(x>0) {
			this.info = "成功";
		}else {
			this.info = "失败";
		}
		request.setAttribute("info", info);
		this.ordersInsert(request, response);
		
	}

	/**
	 * 订单添加提交页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======ordersInsertSubmit============");
		HttpSession session = request.getSession() ; 
		AUsers users = (AUsers) session.getAttribute("users");
		
		/* 车辆id */
		String bid = request.getParameter("bid");
		/* 接收参数, 处理参数,new对象,赋值,调用Service方法,存储结果 */
		/* 终于开始写Orders模块了 */
		/* 只需要准备一下orders即可 */
		AOrders orders = new AOrders() ;
		
		/* 用户id:
		 * 从session中取,
		 *  */
		orders.setUsersId(users.getId());
		orders.setBikeId(Integer.valueOf(bid));
		/* 订单的名称赋值放到Service来做  */
		orders.setStartTime(new Date());
		orders.setRepairContent("");
		
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		Map<String,Object> resultMap = this.ordersService.saveOneorderService(orders);
		this.info = resultMap.get("info") + "";
	
		request.setAttribute("info", info);

		this.ordersInsert(request, response);
	}

	/**
	 * 打开订单添加的页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void ordersInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=====ordersInsert=======");
		this.url = "/WEB-INF/jsp/head/orders/ordersInsert.jsp" ; 
		
		/**
		 * 根据车辆id查询车辆信息
		 */
		String bid = request.getParameter("bid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", bid);
		condMap.put("extends", "true");
		ABike bike = this.bikeService.findOneBikeService(condMap);
		request.setAttribute("bike", bike);
		
		/* 服务器端跳转 */
		request.getRequestDispatcher(url).forward(request, response);
	}
}
